import "package:flutter/material.dart";
import "pages/DiscoveryPage.dart";
import "pages/MyInfoPage.dart";
import "pages/NewsListPage.dart";
import "pages/TweetsListPage.dart";
import "package:flutter/cupertino.dart";
import "pages/NewsDetailPage.dart";
import "widgets/MyDrawer.dart";

void main() {
  runApp(new OSCApp());
}

class OSCApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new OSCAppState();
  }
}

class OSCAppState extends State<OSCApp> {
  int _tabIndex = 0;
  var _body;
  var tabImages;
  var appBarTitles = ["资讯", "动弹", "发现", "我的"];
  // TabItem的文本默认样式
  final tabTextStyleNormal = new TextStyle(color: const Color(0xff969696));
  // TabItem被选中时的文本样式
  final tabTextStyleSelected = new TextStyle(color: const Color(0xff63ca6c));

  Map<String, WidgetBuilder> _routers = new Map();

  void initData() {
    _routers["newsDetail"] = (BuildContext) {
      return new NewsDetailPage();
    };
    if (tabImages == null) {
      tabImages = [
        [
          getTabImage('images/ic_nav_news_normal.png'),
          getTabImage('images/ic_nav_news_actived.png')
        ],
        [
          getTabImage('images/ic_nav_tweet_normal.png'),
          getTabImage('images/ic_nav_tweet_actived.png')
        ],
        [
          getTabImage('images/ic_nav_discover_normal.png'),
          getTabImage('images/ic_nav_discover_actived.png')
        ],
        [
          getTabImage('images/ic_nav_my_normal.png'),
          getTabImage('images/ic_nav_my_pressed.png')
        ]
      ];
    }
    _body = new IndexedStack(
      children: <Widget>[
        new NewsListPage(),
        new TweetsListPage(),
        new DiscoveryPage(),
        new MyInfoPage(),
      ],
      index: _tabIndex,
    );
  }

  Image getTabImage(path) {
    return new Image.asset(
      path,
      width: 20.0,
      height: 20.0,
    );
  }

  List<BottomNavigationBarItem> getBottomBavItems() {
    List<BottomNavigationBarItem> list = new List();
    for (int i = 0; i < 4; i++) {
      list.add(new BottomNavigationBarItem(
          icon: getTabIcon(i), title: getTabTitle(i)));
    }
    return list;
  }

  Image getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  Text getTabTitle(int curIndex) {
    return new Text(
      appBarTitles[curIndex],
      style: getTabTextStyle(curIndex),
    );
  }

  TextStyle getTabTextStyle(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabTextStyleSelected;
    }
    return tabTextStyleNormal;
  }

  @override
  Widget build(BuildContext context) {
    initData();
    return new MaterialApp(
      theme: new ThemeData(primaryColor: const Color(0xFF63CA6C)),
      routes: _routers,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(
            "OSC Client",
            style: new TextStyle(color: Colors.white),
          ),
          iconTheme: new IconThemeData(color: Colors.white),
        ),
        body: _body,        
        bottomNavigationBar: new CupertinoTabBar(
          items: getBottomBavItems(),
          currentIndex: _tabIndex,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ),
        drawer: new myDrawer(),
      ),
    );
  }
}
